package com.qi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qi.entity.Department;
import com.qi.entity.User;
import com.qi.mapper.DepartmentMapper;
import com.qi.mapper.UserMapper;
import com.qi.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qi.utils.DepartmentTree;
import com.qi.vo.query.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingchunlin
 * @since 2024-03-15
 */
@Service
@Transactional
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询部门列表
     *
     * @param departmentQueryVo
     * @return
     */
    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //部门名称查询
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(departmentQueryVo.getDepartmentName()),Department::getDepartmentName,departmentQueryVo.getDepartmentName());
        //排序
        lambdaQueryWrapper.orderByAsc(Department::getOrderNum);
        //查询部门列表
        List<Department> departmentList = departmentMapper.selectList(lambdaQueryWrapper);
        //生成部门树
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, 0L);
        return departmentTree;
    }

    /**
     * 查询上级部门列表
     * * @return
     */
    @Override
    public List<Department> findParentDepartment() {
        //创建条件构造器对象
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<Department>();
        //排序
        lambdaQueryWrapper.orderByAsc(Department::getOrderNum);
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(lambdaQueryWrapper);
        //创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        //生成部门树列表
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        //返回部门列表
        return departmentTree;
    }

    /**
     * 判断部门下是否有子部门
     *
     * @param id
     * @return
     */
    @Override
    public boolean hasChildrenOfDepartment(Long id) {
        //创建条件构造器对象
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<Department>();
        lambdaQueryWrapper.eq(Department::getPid,id);
        //如果数量大于0，表示存在
        if(baseMapper.selectCount(lambdaQueryWrapper)>0){
            return true;
        }
        return false;
    }

    /**
     * 判断部门下是否存在用户
     *
     * @param id
     * @return
     */
    @Override
    public boolean hasUserOfDepartment(Long id) {
        //创建条件构造器对象
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>();
        lambdaQueryWrapper.eq(User::getDepartmentId,id);
        //如果数量大于0，表示存在
        if(userMapper.selectCount(lambdaQueryWrapper)>0){
            return true;
        }
        return false;
    }
}
